package cn.yr.controller.portal;

import cn.hutool.core.util.ObjectUtil;
import cn.yr.common.util.Ret;
import cn.yr.domain.entity.Article;
import cn.yr.domain.vo.ArticleTagVo;
import cn.yr.domain.vo.PageVo;
import cn.yr.service.portal.ArticleService;
import cn.yr.service.portal.ArticleTagService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 文章(Article)表控制层
 *
 * @author lang
 * @since 2020-03-05 20:47:46
 */
@RestController
@RequestMapping("article")
public class ArticleController {

    @Resource
    private ArticleService articleService;
    @Resource
    private ArticleTagService articleTagService;
    /**
     * @author     ：lang
     * @description ： 文章物理分页
     */
    @RequestMapping("getArticlePage")
    public Ret<IPage<ArticleTagVo>> getArticlePage(@RequestBody PageVo<ArticleTagVo> pageVo){
        Map<String, Object> params = pageVo.getParams();
        QueryWrapper<ArticleTagVo> articleQueryWrapper = new QueryWrapper<>();
        articleQueryWrapper.orderByDesc("create_time");
        //按标签查
        Integer tagId = pageVo.getAs("tagId");
        if(ObjectUtil.isNotEmpty(tagId)){
            ArrayList<Integer> ids = new ArrayList<>();
            ids.add(tagId);
            List<Integer> integers = articleTagService.selectArticleIds(ids);
            System.err.println(integers);
            articleQueryWrapper.in("id",integers);
        }
        if(ObjectUtil.isNotEmpty(params.get("type"))){
            articleQueryWrapper.eq("type",params.get("type"));
        }
        articleQueryWrapper.orderByDesc("create_time");
        IPage<ArticleTagVo> articleIPage = articleService.selectByPage(pageVo,articleQueryWrapper);
        return  Ret.success(articleIPage);
    }

    @RequestMapping("getArticlePageByTag")
    public Ret<IPage<ArticleTagVo>> getArticlePageByTag(Integer pageNumber , Integer pageSize,
            @RequestParam(value="tagIdList")List<Integer> tagIdList){
        Page<ArticleTagVo> page = new Page<>(pageNumber,pageSize);
        List<Integer> integers = articleTagService.selectArticleIds(tagIdList);
        QueryWrapper<ArticleTagVo> articleQueryWrapper = new QueryWrapper<>();
        articleQueryWrapper.orderByDesc("create_time");
        articleQueryWrapper.in("id",integers);
        IPage<ArticleTagVo> articleIPage = articleService.selectByPage(page,articleQueryWrapper);
        return  Ret.success(articleIPage);
    }

    /**
     * @author     ：lang
     * @description： 根据ID查询文章
     */
    @RequestMapping("getArticleById")
    public Ret getArticleById(Integer id){
        ArticleTagVo article = articleService.selectById(id);
        return Ret.success(article);
    }
    /**
     * @author     ：lang
     * @description： 推荐的文章
     */
    @RequestMapping("getArticleRecommend")
    public Ret<IPage<Article>> getArticleRecommend(Integer pageNumber , Integer pageSize){
        //默认值1
        pageNumber = ObjectUtil.isEmpty(pageNumber)?1:pageNumber;
        pageSize = ObjectUtil.isEmpty(pageSize)?5:pageSize;
        IPage<Article> articleIPage = articleService.selectByRecommendPage(pageNumber,pageSize);
        return  Ret.success(articleIPage);
    }
}